package com.caojing.syby.ui.cart.cartsharefragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.caojing.syby.R;
import com.caojing.syby.base.BaseFragment;
import com.caojing.syby.bean.home.RowsBean;
import com.caojing.syby.config.UrlConfig;
import com.caojing.syby.ui.adapter.HomeGvAdapter;
import com.caojing.syby.ui.details.productdetail.ClickGridviewItemActivity;
import com.handmark.pulltorefresh.library.LoadingLayoutProxy;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class CartShareFragment extends BaseFragment implements CarfragmentShareConstract.View {

    @Bind(R.id.iv_Not_data)
    ImageView ivNotData;
    private CarfragmentShareConstract.Presenter presenter;

    @Bind(R.id.tenbuysharefragment_pull_gridview)
    PullToRefreshGridView tenbuysharefragmentPullGridview;

    private List<RowsBean> totallist;
    private int PAGES = 1;
    private LoadingLayoutProxy llProxy;//设置刷新时的文本等的对象
    private HomeGvAdapter adapter;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tenbuy_share;   //这里复用了tenbuy里面的布局
    }

    //todo：这里主要是为了fragment的复用
    public static CartShareFragment newInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString("bc", argument);
        CartShareFragment cartShareFragment = new CartShareFragment();
        cartShareFragment.setArguments(bundle);
        return cartShareFragment;
    }

    public Map<String, String> initParams() {
//        这里需要将tenbuyfragment中的数据传递给CHANNEL。这个属性
        Bundle bundle = getArguments();
        String bc = bundle.getString("bc");
        Log.i("tag", "需要的tag" + bc);

        Map<String, String> params = new HashMap<>();
        params.put(UrlConfig.CarfragmentKey.ACT, UrlConfig.CarfragmentDefaultValue.ACT);
        params.put(UrlConfig.CarfragmentKey.PAGES, PAGES + "");
        params.put(UrlConfig.CarfragmentKey.BC, bc);
        params.put(UrlConfig.CarfragmentKey.BRANDID, UrlConfig.CarfragmentDefaultValue.BRANDID);
        params.put(UrlConfig.CarfragmentKey.V, UrlConfig.CarfragmentDefaultValue.V);
        return params;

    }


    @Override
    protected void initView(View view) {
        presenter = new CarfragmentSharePresenter(this);
//        presenter.getVertical(initParams());

        totallist = new ArrayList<>();

        adapter = new HomeGvAdapter(totallist, getContext());
        tenbuysharefragmentPullGridview.setAdapter(adapter);
        tenbuysharefragmentPullGridview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                tenbuysharefragmentPullGridview.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                PAGES++;
                presenter.getVertical(initParams());
                tenbuysharefragmentPullGridview.onRefreshComplete();
            }
        });


        tenbuysharefragmentPullGridview.setScrollingWhileRefreshingEnabled(true);
        llProxy = (LoadingLayoutProxy) tenbuysharefragmentPullGridview.getLoadingLayoutProxy(true, false);

        llProxy.setRefreshingLabel("下拉刷新");//正在刷新时的文本
        llProxy.setReleaseLabel("放开....");// 设置控件拉到一定的距离时显示的文本
        //设置加载的图片
        llProxy.setLoadingDrawable(getResources().getDrawable(R.mipmap.pull_refresh_up));

        //指定刷新的模式 BOTH具有上下拉刷新  PULL_FROM_START 下拉   PULL_FROM_END上拉
        tenbuysharefragmentPullGridview.setMode(PullToRefreshBase.Mode.BOTH);


    }

    @Override
    protected void initData() {
        tenbuysharefragmentPullGridview.setEmptyView(ivNotData);
        presenter.getVertical(initParams());


        //        GridView第点击事件
        tenbuysharefragmentPullGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            private RowsBean rowsBean;

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getContext(), ClickGridviewItemActivity.class);//这里复用了tenbuysharefragment的点击跳转的activity
                RowsBean rowsBean = totallist.get(i);
                if (rowsBean != null) {
                    String spreadUrl = rowsBean.getSpreadUrl();
                    String name = rowsBean.getName();
                    intent.putExtra("name",name);
                    intent.putExtra("spreadUrl", spreadUrl);
                    startActivity(intent);
                }

            }
        });


    }


    @Override
    public void onGetVerticalSucess(List<RowsBean> rowsBeanList) {
        totallist.addAll(rowsBeanList);
        adapter.notifyDataSetChanged();
        tenbuysharefragmentPullGridview.onRefreshComplete();
    }

    @Override
    public void onGetVerticalFail(String erro) {
        Log.i("tag", erro);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }
}
